package www.lagou.app;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import www.lagou.entity.Phone;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author wanggege
 * 编程题2
 */
public class Demo02Test {

    /**
     * @throws SQLException
     * 需求1:  查询价格高于2000元，生产日期是2019年之前的所有手机
     */
    @Test
    public void queryTest1() throws SQLException {

        //使用连接池创建QueryRunner对象 自动模式
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //编写查询SQL
        String sql = "select * from phone where price > ? and prodate < ?";
        //设置占位符
        Object[] param = {2000.0, "2019-01-01"};

        List<Phone> phoneList = qr.query(sql, new BeanListHandler<Phone>(Phone.class), param);

        for (Phone phone : phoneList) {
            System.out.println(phone);
        }


    }


    /**
     * @throws SQLException
     * 需求2:  查询所有颜色是白色的手机信息
     */
    @Test
    public void queryTest2() throws SQLException {

        //使用连接池创建QueryRunner对象 自动模式
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //编写查询SQL
        String sql = "select * from phone where color like ? ";
        //设置占位符
        Object[] param = {"%白%"};

        List<Phone> phoneList = qr.query(sql, new BeanListHandler<Phone>(Phone.class), param);

        for (Phone phone : phoneList) {
            System.out.println(phone);
        }


    }


}
